package com.atxwq.shop.manager.controller;

import com.atxwq.shop.manager.serivce.SysMenuService;
import com.atxwq.shop.manager.serivce.SysUserService;
import com.atxwq.shop.manager.serivce.ValidateCodeService;
import com.atxwq.shop.model.dto.system.LoginDto;
import com.atxwq.shop.model.entity.system.SysMenu;
import com.atxwq.shop.model.entity.system.SysUser;
import com.atxwq.shop.model.vo.common.Result;
import com.atxwq.shop.model.vo.common.ResultCodeEnum;
import com.atxwq.shop.model.vo.system.LoginVo;
import com.atxwq.shop.model.vo.system.SysMenuVo;
import com.atxwq.shop.model.vo.system.ValidateCodeVo;
import com.atxwq.utils.AuthContextUtil;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 登录持久层
 * @Date 2025/5/7 16:09
 * @Created by 12336
 */

@Tag("电商实战项目")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ValidateCodeService validateCodeService;
    /**
     * 登录方法
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取验证码
     * @return
     */
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> getValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.getValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据token获取用户信息
     * @return
     */
//    @GetMapping("/getUserInfo")
//    public Result getUserInfo(@RequestHeader("token") String token){
//        SysUser sysUser =  sysUserService.getUserInfo(token);
//        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
//    }
    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }

    /**
     * 退出登录
     * @param token
     * @return
     */
    @GetMapping("/logout")
    public Result logout(@RequestHeader("token") String token){
         sysUserService.logout(token);
         return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户菜单
     * @return
     */
    @GetMapping("/menus")
    public Result menus(){
        List<SysMenuVo> sysMenuList = sysMenuService.findMenuList();
        return Result.build(sysMenuList, ResultCodeEnum.SUCCESS);
    }

}
